package com.example.administrator.a62303;

import android.app.assist.AssistStructure;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private List<Msg> msgList=new ArrayList<>();

    private EditText inputText;
    private Button  send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initMsgs();

        inputText=(EditText)findViewById(R.id.input_text);
        send     =(Button)findViewById(R.id.send);
        msgRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter=new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content=inputText.getText().toString();
                if(!"".equals(content)){

                    Msg msg=new Msg(content,Msg.TYPE_SEND);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size()-1);
                    msgRecyclerView.scrollToPosition(msgList.size()-1);
                    inputText.setText("");


                }
            }
        });




    }


private void initMsgs(){

    Msg msg1=new Msg("hello guy.",Msg.TYPE_RECEIVED);
    msgList.add(msg1);
    Msg msg2=new Msg("Hello Who is that",Msg.TYPE_SEND);
    msgList.add(msg2);
    Msg msg3=new Msg("THis is tom,Nice talking to you.",Msg.TYPE_RECEIVED);
    msgList.add(msg3);



}
    private String getRandomLengthName(String name) {

        Random random=new Random();
        int length=random.nextInt(20)+1;
        StringBuilder builder=new StringBuilder();
        for(int i=0;i< length ;i++){

            builder.append(name);
        }
        return builder.toString();
    }
}
